<?php

namespace App\Transformers;


use App\Models\Message;
use App\Models\Student;
use League\Fractal\TransformerAbstract;
use App\Models\Coach;
use App\Models\School;

class MessageTransformer extends TransformerAbstract
{

    public function transform(Message $item)
    {
        if($item->from_type == 1){
            $from_name = School::find($item->from_id)->shortname;
            $headimg_url = '';
        }elseif($item->from_type == 2){
            $coach_info = Coach::find($item->from_id);
            $from_name = $coach_info->name;
            $headimg_url = $coach_info->photo;
        }else{
            $stu_info = Student::find($item->from_id);
            $from_name = $stu_info->name;
            $headimg_url = $stu_info->photo;
        }
        return[
            "id"=>$item->id,
            "title"=>$item->content,
            "content"=>$item->content,
            "fromuser"=>[
                "userid"=>$item->from_id,
                "nickname"=>$from_name,
                "modelid"=>$item->from_type,
                "sex"=>"",
                "userpic"=> $headimg_url,
            ],
            "touser"=>$item->to_id,
            "IP"=>"",
            "addtime"=>strtotime($item->created_at),
            "isread"=>"1",
            "status"=>$item->id,
        ];




    }


}
